﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TA.Entities;
using System.Web;
using System.Collections;
using System.Web.Caching;

namespace TA.WebUtils
{
	public class WebCache : IDataCache
	{
		public void Add(string key, object data, DateTime expiry)
		{
			Cache.Add(key, data, null, expiry, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
		}
		public void Clear()
		{

		}
		public void Remove(string key)
		{
			Cache.Remove(key);
		}
		public object this[string key]
		{
			get
			{
				return Cache[key];
			}
			set
			{
				this.Add(key, value, System.Web.Caching.Cache.NoAbsoluteExpiration);
			}
		}
		public IEnumerator<string> GetEnumerator()
		{
			return new Enumerator(this);
		}
		IEnumerator IEnumerable.GetEnumerator()
		{
			return new Enumerator(this);
		}
		protected Cache Cache
		{
			get
			{
				return HttpContext.Current.Cache;
			}
		}
		public class Enumerator : IEnumerator<string>
		{
			public Enumerator(WebCache wc)
			{
				_de = wc.Cache.GetEnumerator();
			}
			IDictionaryEnumerator _de;

			public string Current
			{
				get { return (string)this._de.Key; }
			}

			public void Dispose()
			{

			}

			object System.Collections.IEnumerator.Current
			{
				get { return this._de.Key; }
			}

			public bool MoveNext()
			{
				return this._de.MoveNext();
			}

			public void Reset()
			{
				this._de.Reset();
			}
		}
	}
}
